import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  ParseIntPipe, Put,
} from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { User } from './user.entity';
import { UsersService } from './users.service';
import { ReturnUserDto } from './dto/return-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Public } from '../auth/decorators/public.decorator';

@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}
  @Public()
  @Post()
  create(@Body() createUserDto: CreateUserDto): Promise<User> {
    return this.usersService.create(createUserDto);
  }

  @Get()
  findAll(): Promise<User[]> {
    return this.usersService.findAll();
  }

  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number): Promise<User> {
    return this.usersService.findOne(id);
  }

  @Get('getUserInfo/:name')
  async findOneByName(@Param('name') name: string): Promise<ReturnUserDto> {
    const returnUserDto = new ReturnUserDto()
    const user =await this.usersService.findOneByName(name)
    returnUserDto.id = user.id
    returnUserDto.userName = user.userName
    returnUserDto.email = user.email
    returnUserDto.phonenumber = user.phonenumber
    returnUserDto.sex = user.sex
    returnUserDto.avatar = user.avatar
    returnUserDto.createTime = user.createTime
    return returnUserDto;
  }

  @Get('updatePass/:name/:oldPass')
  async updatePass(@Param('name') name: string,@Param('oldPass') oldPass: string): Promise<boolean> {
    const user =await this.usersService.findOneByName(name)
    return user.password==oldPass;
  }

  @Delete(':id')
  remove(@Param('id') id: string): Promise<void> {
    return this.usersService.remove(id);
  }

  @Put(':id')
  update(@Param('id', ParseIntPipe) id: number, @Body() updateUserDto: UpdateUserDto): Promise<User> {
    return this.usersService.update(id, updateUserDto);
  }

}